package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper roleMapper;

    @Override
    public Set<String> findRolesByRoleIds(Integer[] roleIds) {
//        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
//        //根据id  in查询
//        wrapper.in(DtsRole::getId , roleIds);
//        // 查询没有被逻辑删除
//        wrapper.eq(DtsRole::getDeleted, false);
//
//        List<DtsRole> roleList = roleMapper.selectList(wrapper);
//
//        // stream 流进行转换数据
//        Set<String> roles = roleList.stream().map((role) -> {
//            return role.getName();
//        }).collect(Collectors.toSet());
//
//        return roles;
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        //根据id  in查询
        wrapper.in(DtsRole::getId, roleIds);
        // 查询没有被逻辑删除
        wrapper.eq(DtsRole::getDeleted, false);

        List<DtsRole> roleList = roleMapper.selectList(wrapper);

        // stream 流进行转换数据
        Set<String> roles = roleList.stream().map((role) -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return roles;
    }

    @Override
    public List<DtsRole> findAll() {
//        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(DtsRole::getDeleted,false);
//        List<DtsRole> list = roleMapper.selectList(wrapper);
//        return list;
        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsRole::getDeleted, false);

        List<DtsRole> roleList = roleMapper.selectList(wrapper);
        return roleList;
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole> iPage = new Page(page, limit);

        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)) {
            wrapper.like("name", rolename);
        }
        if (!StringUtils.isEmpty(order) && StringUtils.isEmpty(sort)) {
            if (!"desc".equals(order)) {
                wrapper.orderByDesc(sort);
            } else {
                wrapper.orderByAsc(sort);
            }
        }

        roleMapper.selectPage(iPage, wrapper);
        //封装结果集
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }

    @Override
    public void create(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);
        roleMapper.insert(role);
    }

    @Override
    public DtsRole updateRole(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setName(role.getName());
        role.setDesc(role.getDesc());
        role.setDeleted(false);
        role.setEnabled(true);
        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        roleMapper.updateById(role);

        return role;
    }

    @Autowired
    DtsPermissionMapper permissionMapper;
    @Autowired
    DtsAdminMapper dtsAdminMapper;

    @Override
    public void delete(DtsRole role) {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getRoleIds, role.getId());
        Integer integer = dtsAdminMapper.selectCount(wrapper);
        if (integer > 0) {
            throw new RuntimeException("数据被管理不能删除");
        }

        LambdaQueryWrapper<DtsPermission> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsPermission::getRoleId, role.getId());
        Integer integer1 = permissionMapper.selectCount(wrapper1);
        if (integer1 > 0) {
            throw new RuntimeException("数据被关联不能被删除");
        }
        roleMapper.deleteById(role);
    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole dtsRole = roleMapper.selectById(roleId);
        return dtsRole.getName().equals("超级管理员");
    }

    @Override
    public Set<String> queryStringPermissions(Integer roleId) {
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsPermission::getRoleId, roleId);
        wrapper.eq(DtsPermission::getDeleted, false);
        List<DtsPermission> permissionList = permissionMapper.selectList(wrapper);

        //数据转换
        Set<String> perms = permissionList.stream().map((permission) -> {
            return permission.getPermission();
        }).collect(Collectors.toSet());
        return perms;
    }

}
